/**
 * author      : dormi330
 * date        : 2018/6/24
 * project     : mybatis
 * description : 订单 data access service, 如果有缓存, 则在这一层实现
 */

package com.deep.trade.infrastructure.mybatis.order;

import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.domain.order.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MybatisOrderRepository implements OrderRepository {

    @Autowired
    private AppRuntimeEvn appRuntimeEvn;

    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Autowired
    private OrderSummaryMapper orderSummaryMapper;

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    @Autowired
    private TradeOrderSkuMapper tradeOrderSkuMapper;

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;

    @Autowired
    private OrderParcelMapper orderParcelMapper;

    @Autowired
    private TradeOrderHistoryMapper historyMapper;

    public void addOrderSummary(OrderSummary orderSummary) {
        orderSummary.setTenantCode(appRuntimeEvn.getTenantCode());
        orderSummaryMapper.insert(orderSummary);
    }

    public void addTradeOrder(TradeOrder tradeOrder) {
        tradeOrder.setTenantCode(appRuntimeEvn.getTenantCode());
        tradeOrderMapper.insert(tradeOrder);

        // 记录历史
        backupTradeOrderHistory(TradeOrderHistory.fromTradeOrder(tradeOrder));
    }

    public void backupTradeOrderHistory(TradeOrderHistory history) {
        history.setTenantCode(appRuntimeEvn.getTenantCode());
        historyMapper.insert(history);
    }


    public void addOrderSku(TradeOrderSku tradeOrderSku) {
        tradeOrderSku.setTenantCode(appRuntimeEvn.getTenantCode());
        tradeOrderSkuMapper.insert(tradeOrderSku);
    }

    public void addDeliveryAddress(DeliveryAddress deliveryAddress) {
        deliveryAddress.setTenantCode(appRuntimeEvn.getTenantCode());
        deliveryAddressMapper.insert(deliveryAddress);
    }

    public void addOrderPayment(OrderPayment orderPayment) {
        orderPayment.setTenantCode(appRuntimeEvn.getTenantCode());
        orderPaymentMapper.insert(orderPayment);
    }

    public void addParcel(OrderParcel orderParcel) {
        orderParcel.setTenantCode(appRuntimeEvn.getTenantCode());
        orderParcelMapper.insert(orderParcel);
    }

    public int deleteTradeOrder(TradeOrder tradeOrder) {
        int count = tradeOrderMapper.deleteById(
                tradeOrder.getTenantCode(),
                tradeOrder.getId(),
                tradeOrder.getVersion()
        );

        // 记录历史
        if (count == 1) {
            tradeOrder.setDr(TradeOrder.DR_DELETED);
            tradeOrder.increaseVersion();
            backupTradeOrderHistory(TradeOrderHistory.fromTradeOrder(tradeOrder));
        }

        return count;
    }

    public int updateTradeOrderStatus(TradeOrder tradeOrder) {
        int count = tradeOrderMapper.updateStatus(
                /* condition */
                tradeOrder.getTenantCode(),
                tradeOrder.getId(),
                tradeOrder.getVersion(),
                /* update fields */
                tradeOrder.getStatus(),
                tradeOrder.getPreStatus(),
                tradeOrder.getAuditRejectReason(),
                tradeOrder.getAuditedAt(),
                tradeOrder.getUpdatedBy()
        );

        // 记录历史
        if (count == 1) {
            tradeOrder.increaseVersion();
            backupTradeOrderHistory(TradeOrderHistory.fromTradeOrder(tradeOrder));
        }

        return count;
    }

    public int updateOrderSummaryToPaid(OrderSummary orderSummary) {
        return orderSummaryMapper.updateStatus(
                // condition
                orderSummary.getTenantCode(),
                orderSummary.getId(),
                orderSummary.getVersion(),
                // change field
                orderSummary.getPaidAt(),
                orderSummary.getPayChannel(),
                orderSummary.getUpdatedBy());
    }

}
